// manages sounds

#define ENABLE_SOUNDS              1

#if ENABLE_SOUNDS

#include <stdio.h>
#include <irrKlang.h>
#include <assert.h>
#include <direct.h>
#include <string>

#include "soundManager.h"

using namespace std;

// all irrklang functionality is in this namespace
using namespace irrklang;

// link with library
#pragma comment(lib, "irrKlang.lib")


//--- GLOBALS ---//
static ISoundEngine* engine = NULL;
static char *bgmFiles[NUM_SOUND_BGM];
static char *sfxFiles[NUM_SOUND_SFX];

void initSounds() {
	// start the sound engine with default parameters
	engine = createIrrKlangDevice();

	bgmFiles[BGM_NONE]   = "";
	bgmFiles[BGM_TITLE]   = "assets/sound/bgm/8bp088-01-nullsleep-fearless_flight.mp3";
	bgmFiles[BGM_CUTSCENE]   = "assets/sound/bgm/8bp088-04-nullsleep-valentine_final.mp3";
	bgmFiles[BGM_STAGE]  = "assets/sound/bgm/01_LavaMix.wav";
	bgmFiles[BGM_ENDING] = "assets/sound/bgm/02_arcs2000.wav";


	sfxFiles[SFX_GAME_OVER] = "assets/sound/sfx/GAME_OVER_YEAA.mp3";

	sfxFiles[SFX_WALKING] = "assets/sound/sfx/Footsteps_Walking_SoundBible.com.mp3";
	sfxFiles[SFX_ILLUSION_SOUND] = "assets/sound/sfx/Magic_Wand_Noise_SoundBible.com.mp3";
	sfxFiles[SFX_PLAYER_DEATH] = "assets/sound/sfx/Scream_And_Die_FX_SoundBible.com.mp3";
	sfxFiles[SFX_LEVEL_VICTORY] = "assets/sound/sfx/Goofy_Laugh_SoundBible.com.mp3";

	sfxFiles[SFX_GUARD_SEES_BODY] = "assets/sound/sfx/Child_Scream_SoundBible.com.mp3";

	sfxFiles[SFX_DOG_DIES] = "assets/sound/sfx/Sickness_SoundBible.com.mp3";
	sfxFiles[SFX_DOG_DISCOVERS_PLAYER] = "assets/sound/sfx/Barking_SoundBible.com.mp3";
	sfxFiles[SFX_GUARD_KILLS_GUARD] = "assets/sound/sfx/Cal_Gun_Shot__SoundBible.com.mp3";
	sfxFiles[SFX_GUARD_DISCOVERS_PLAYER] = "assets/sound/sfx/Shotgun_Cock_And_Shoot_SoundBible.com.mp3";
}

void playBgm(SoundBgm bgm) {
	assert(bgm >= 0 && bgm < NUM_SOUND_BGM);

	engine->stopAllSounds();
	engine->removeAllSoundSources();

	if (bgm == BGM_NONE) {
		return;
	}

	engine->play2D(bgmFiles[bgm], true);
}

void playSfx(SoundSfx sfx) {
	assert(sfx >= 0 && sfx < NUM_SOUND_SFX);
	engine->play2D(sfxFiles[sfx]);
}

// tests all of the sounds in the sound library
void soundTest() {
	char c;

	printf("press ENTER to move on to the next sound\n");
	printf("a message will print after the last sound\n\n");

	for (int i = 0; i < NUM_SOUND_BGM; i++) {
		if (i == BGM_NONE) {
			continue;
		}

		playBgm((SoundBgm)i);
		c = getchar();
	}

	for (int i = 0; i < NUM_SOUND_SFX; i++) {
		playSfx((SoundSfx)i);
		c = getchar();
	}

	printf("sound test complete\n");
}

// shut down sound engine
void shutdownSounds() {
	if (engine != NULL) {
		engine->drop();
	}
}

#else

#include "soundManager.h"

void initSounds() {}

void playBgm(SoundBgm bgm) {}

void playSfx(SoundSfx sfx) {}

// tests all of the sounds in the sound library
void soundTest() {}

void shutdownSounds() {}

#endif
// end of soundManager.cpp
